package com.ykjndz.bt.adapter;

import java.util.ArrayList;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ykjndz.bt.R;
import com.ykjndz.bt.tool.ViewHolder;

public class DeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;

    private ArrayList<Integer>         arrayList;
    private Context                    mContext;

    public DeviceListAdapter(Context context, ArrayList<Integer> arrayList) {
        super();
        this.arrayList = arrayList;
        mLeDevices = new ArrayList<BluetoothDevice>();
        this.mContext = context;
    }

    public void addDevice(BluetoothDevice device, int rssi) {
        if (!mLeDevices.contains(device)) {
            mLeDevices.add(device);
            arrayList.add(rssi);
        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        if (mLeDevices != null)
            mLeDevices.clear();
        if (arrayList != null)
            arrayList.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices == null ? 0 : mLeDevices.size();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        return mLeDevices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.adapter_device, null);
        }
        TextView deviceAddress = ViewHolder.get(convertView, R.id.tv_deviceAddr);
        TextView deviceName = ViewHolder.get(convertView, R.id.tv_deviceName);
        // TextView rssi = ViewHolder.get(convertView, R.id.tv_rssi);

        BluetoothDevice device = getItem(position);
        deviceAddress.setText(device.getAddress());
        deviceName.setText(device.getName());
        // rssi.setText("" + arrayList.get(i));
        return convertView;
    }
}
